package servlet;

import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import dao.Dao;
import domain.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/updateUserServlet")
public class UpdateUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        String username = request.getParameter("username");
        String gender = request.getParameter("gender");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");
        User user = new User();
        user.setId(Integer.valueOf(id));
        user.setUsername(username);
        user.setGender(gender);
        user.setPhone(phone);
        user.setAddress(address);
        Dao dao = new Dao();
        int i = dao.updateUser(user);
        if(i>0){
            User userById = dao.findUserById(Integer.valueOf(id));
            request.getSession().setAttribute("user_login",userById);
            response.sendRedirect(request.getContextPath()+"/index.jsp");

        }else{
            request.setAttribute("updateUser_info","修改失败");
            request.getRequestDispatcher("/updateUser.jsp").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
